package logic

import (
	"context"
	"fmt"
	"srv/model/mysql"
	"srv/model/redisinit"

	"srv/internal/svc"
	"srv/srv"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpLogic {
	return &UserUpLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserUpLogic) UserUp(in *srv.UserUpRequest) (*srv.UserUpResponse, error) {
	// todo: add your logic here and delete this line

	u := mysql.User{}
	err := u.UserInfo(in.Tel)
	if err != nil {
		return nil, fmt.Errorf("用户不存在")
	}
	get := redisinit.Rdb.Get(context.Background(), "sendsms"+in.Tel+"userup")
	if get.Val() != in.SmCode {
		return nil, fmt.Errorf("短信验证码有误")
	}

	return &srv.UserUpResponse{
		Id: int64(u.ID),
	}, nil
}
